//
//  WildDogService.m
//  SideDemo
//
//  Created by zsp on 16/4/26.
//  Copyright © 2016年 zsp. All rights reserved.
//

#import "WildDogService.h"
#import <Wilddog/Wilddog.h>

#ifndef WILDDOGURL
#define WILDDOGURL @"https://wild-hare-97693.wilddogio.com/"
#endif

@interface WildDogService ()

@property(nonatomic, strong)Wilddog *wilddog;
@property(nonatomic, strong)NSMutableArray *observers;

@end

@implementation WildDogService

+(WildDogService*)shareInstance {
    static dispatch_once_t onceToken;
    static WildDogService *service;
    dispatch_once(&onceToken, ^{
        if (service == nil) {
            service=[WildDogService new];
        }
    });
    return service;
}

- (instancetype)init {
    if (self=[super init]) {
        _wilddog=[[Wilddog alloc]initWithUrl:WILDDOGURL];
        _observers=[NSMutableArray array];
        [_wilddog observeEventType:WEventTypeValue withBlock:^(WDataSnapshot *snapshot) {
            NSLog(@"%@ => %@", snapshot.key, snapshot.value);
//            [_observers enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                
//            }];
        }];
        
        [_wilddog observeAuthEventWithBlock:^(WAuthData *authData) {
            if (authData) {
                NSLog(@"authed: %@",authData.uid);
            }else{
                NSLog(@"no authed...");
            }
        }];
    }
    return self;
}

- (void)saveData:(NSString*)data {
//    [self.wilddog setValue:data];
    NSDictionary *alanisawesome = @{
                                    @"full_name" : @"Alan Turing",
                                    @"date_of_birth": @"June 23, 1912"
                                    };
    NSDictionary *gracehop = @{
                               @"full_name" : @"Grace Hopper",
                               @"date_of_birth": @"December 9, 1906"
                               };
    Wilddog *usersRef = [self.wilddog childByAppendingPath: @"users"];
    NSDictionary *users = @{
                            @"alanisawesome": alanisawesome,
                            @"gracehop": gracehop
                            };
    [usersRef setValue: users];
}

- (void)updateData:(NSString*)data {
    Wilddog *hopperRef = [self.wilddog childByAppendingPath: @"users/gracehop"];
    
    NSDictionary *nickname = @{
                               @"nickname": @"Amazing Grace",
                               };
    
    [hopperRef updateChildValues: nickname];
}

- (void)postsData:(NSString*)data {
    Wilddog *ref=self.wilddog;
    Wilddog *postRef = [ref childByAppendingPath: @"posts"];
    NSDictionary *post1 = @{
                            @"author": @"gracehop",
                            @"title": @"Announcing COBOL, a New Programming Language"
                            };
    Wilddog *post1Ref = [postRef childByAutoId];
    [post1Ref setValue: post1];
    
    NSDictionary *post2 = @{
                            @"author": @"alanisawesome",
                            @"title": @"The Turing Machine"
                            };
    Wilddog *post2Ref = [postRef childByAutoId];
    [post2Ref setValue: post2];
}

/**
 *  增加数据发生变化时的监听器
 *
 *  @param object <#object description#>
 */
- (void)addValueChangedObserver:(NSObject*)object {
    [self.observers addObject:object];
}

/**
 *  移除数据发生变化时的监听器
 *
 *  @param object <#object description#>
 */
- (void)removeValueChangedObserver:(NSObject*)object {
    [self.observers removeObject:object];
}

@end
